package io.chenhd.gateway.router;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮询算法
 */
public class RoundHttpEndpointRouter implements HttpEndpointRouter {

    private AtomicInteger count;

    private RoundHttpEndpointRouter() {
        this.count = new AtomicInteger(0);
    }

    public static RoundHttpEndpointRouter getInstance() {
        return SingleRoundRouter.INSTANCE;
    }

    private static class SingleRoundRouter {
        private static RoundHttpEndpointRouter INSTANCE = new RoundHttpEndpointRouter();
    }

    @Override
    public String route(List<String> urls) {
        return urls.get(count.incrementAndGet() % urls.size());
    }
}
